package Repositorio;

import Engine.Codec;
import LinkDB.Conexao;
import View.Login;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RepositorioLogin {

    private Connection conn;
    private String query;
    private PreparedStatement stmt;
    private ResultSet rs;

    public boolean getValidLogin() throws SQLException {
        String password = Codec.encodeText(Login.passwordLogin.getText());
        conn = Conexao.getConexao();
        query = "SELECT * FROM login WHERE nameLogin = ? && passwordLogin = ?";
        stmt = conn.prepareStatement(query);
        stmt.setString(1, Login.nameLogin.getText());
        stmt.setString(2, password);
        rs = stmt.executeQuery();
        if (rs.next()) {
            rs.close();
            stmt.close();
            conn.close();
            return true;
        } else {
            rs.close();
            stmt.close();
            conn.close();
            return false;
        }

    }

    public int idLogin(String nameLogin) throws SQLException {
        conn = LinkDB.Conexao.getConexao();
        query = "SELECT idLogin FROM login WHERE nameLogin = ?";
        stmt = conn.prepareStatement(query);
        stmt.setString(1, nameLogin);
        rs = stmt.executeQuery();
        if (rs.next()) {
            return Integer.parseInt(rs.getString("idLogin"));
        } else {
            return 0;
        }
    }
}
